package com.zj.util;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;
import org.codehaus.jackson.JsonNode;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.type.TypeReference;

import java.io.File;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class JsonUtil {

    private static ObjectMapper mapper = new ObjectMapper();
    private static Pattern p = Pattern.compile("\\s*|\t|\r|\n");
    /**
     * 将JSON字符串转换为对象
     *
     * @param json
     *            JSON字符串
     * @param valueType
     *            对象类型
     * @return 对象
     */
    public static <T> T toObject(String json, Class<T> valueType) {
        if(StringUtils.isEmpty(json) || valueType==null){
            return null;
        }
        try {
            return mapper.readValue(json, valueType);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static JsonNode toJsonObject(String json) {
        if(StringUtils.isEmpty(json) ){
            return null;
        }
        try {
            return mapper.readTree(json);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static <T> T toObject(String json, TypeReference<?> typeReference) {
        if(StringUtils.isEmpty(json) || typeReference==null){
            return null;
        }
        try {
            return mapper.readValue(json, typeReference);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String obj2String(Object obj){
        try {
            if(obj==null){
                return null ;
            }
            return mapper.writeValueAsString(obj);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String obj2PrettyString(Object obj){
        try {
            return mapper.writerWithDefaultPrettyPrinter().writeValueAsString(obj);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static <T> T prettyToObject(String json, Class<T> valueType) {
        if(StringUtils.isEmpty(json) || valueType==null){
            return null;
        }
        try {
            Matcher m = p.matcher(json);
            json = m.replaceAll("");
            return mapper.readValue(json, valueType);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static <T> T prettyFileToObject(File jsonFile, Class<T> valueType) {
        String json = null;
        try {
            json = FileUtils.readFileToString(jsonFile, "utf-8");
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
        if(StringUtils.isEmpty(json) || valueType==null){
            return null;
        }
        try {
            Matcher m = p.matcher(json);
            json = m.replaceAll("");
            return mapper.readValue(json, valueType);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
